<template>
  <div class="app-container">
    <div class="left-bar">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="工程编号" prop="projectId">
          <el-input
            v-model="queryParams.projectId"
            placeholder="请输入工程编号"
            clearable
            :style="{width: '100%'}"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            :disabled="single"
            @click="add"
          >添加</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="leftData"  @selection-change="saveLeft" ref="leftData" >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="小区" align="center" prop="areaName" />
        <el-table-column label="楼宇" align="center" prop="groupName" />
        <el-table-column label="单元" align="center" prop="unitName" />
        <el-table-column label="户名称" align="center" prop="doorName" />
        <el-table-column label="房屋编号" align="center" prop="houseId" />
        <el-table-column align="center" label="面积" prop="space"/>
        <el-table-column align="center" label="预缴金额" prop="budgetMoney"/>
        <el-table-column align="center" label="账户余额" prop="balance"/>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <div class="right-bar">
      <div class="shan" stype="margin-top:500px;">
        <el-row :gutter="10" class="mb8" >
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              :disabled="multiple"
              @click="remove"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              @click="handleCalculate"
            >试算</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              plain
              type="success"
              icon="el-icon-download"
              @click="handleUpdate"
            >修改</el-button>
          </el-col>
        </el-row>
      </div>
      <el-table :cell-style="tableRowClassName" v-loading="loading" :data="rightData" @selection-change="saveRight" ref="rightData">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="房屋编号" align="center" prop="houseId" />
        <el-table-column align="center" label="分摊金额" prop="repairMoney"/>
        <el-table-column align="center" label="不足金额" prop="noMoney"/>
        <el-table-column align="center" label="拨付金额" prop="payMoney"/>
      </el-table>
    </div>

    <!-- 添加或修改户资金修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工程编号" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="工程造价" prop="projectMoney">
          <el-input v-model="form.projectMoney" placeholder=""/>
        </el-form-item>
        <el-form-item label="试算单价" prop="price">
          <el-input v-model="form.price" placeholder=""/>
        </el-form-item>
        <el-form-item label="核算造价" prop="trueMoney">
          <el-input v-model="form.trueMoney" placeholder=""/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="trialCalculateForm">试算</el-button>
        <el-button type="primary" @click="checkCalculateForm">核算</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="titleDetail" :visible.sync="openDetail" append-to-body width="500px">
      <el-form ref="formDetail" :model="formDetail" :rules="rules" label-width="80px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="formDetail.houseId" placeholder="" :disabled="true"/>
        </el-form-item>
        <el-form-item label="分摊金额" prop="repairMoney">
          <el-input v-model="formDetail.repairMoney" placeholder=""/>
        </el-form-item>
        <el-form-item label="不足金额" prop="noMoney" >
          <el-input v-model="formDetail.noMoney" placeholder="" :disabled="true"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRepairDoor,
  listRepairRight,
  getRepairDoor,
  delRepairDoor,
  insertRepairDoor,
  updateRepairDoor
} from "@/api/repair/repairDoor";
import {getCalculate, updateCalculate} from "../../../api/repair/repairDoor";

export default {
  name: "RepairDoor",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单元维修明细表表格数据
      leftData: [],
      rightData: [],
      // 弹出层标题
      title: "",
      titleDetail: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
      },
      // 表单参数
      form: {},
      formDetail: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    tableRowClassName({row}){
      return (row.balance<=row.budgetMoney*0.3?'color: red;':"")
    },

    getList() {
      this.loading = true;
      listRepairDoor(this.queryParams).then(response => {
        this.leftData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listRepairRight(this.queryParams).then(response => {
        this.rightData = response.rows;
      });
    },
/*    rightGetList() {
      debugger
      this.loading = true;
      listRepairRight(this.queryRightParams).then(response => {
        this.rightData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },*/
    // 取消按钮
    cancel() {
      this.open = false;
      this.openDetail = false;
      this.reset();
      this.resetDetail();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        doorId: null,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
        houseId: null,
        space: null,
        budgetMoney: null,
        repairMoney: null,
        noMoney:null,
        afterBalance: null,
        payMoney: null,
        balance: null
      };
    },
    resetDetail() {
      this.formDetail = {
        projectId: null,
        houseId: null,
        repairMoney: null,
        noMoney: null
      };
      this.resetForm("formDetail");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 重置按钮操作 */
    resetRightQuery() {
      this.resetForm("queryRightForm");
      this.rightList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.houseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    saveLeft(rows) {
      this.ids = rows.map(item => item.houseId)
      this.single = !rows.length>=1
      this.multiple = !rows.length
      this.left = [];
      if (rows) {
        rows.forEach(row => {
          if (row) {
            this.left.push(row);
          }
        });
      }
    },

    saveRight(rows) {
      this.ids = rows.map(item => item.houseId)
      this.projectId = rows.map(item => item.projectId)
      this.single = rows.length!==1
      this.multiple = !rows.length
      this.right = [];
      if (rows) {
        rows.forEach(row => {
          if (row) {
            this.right.push(row);
          }
        });
      }
    },
    // 右边表格选择项移到左边
    remove() {
      debugger
      this.houseIds = []
      this.projectId=this.queryParams.projectId
      this.rightData.forEach(item=>{
        this.houseIds.push(item.houseId)
      })
      delRepairDoor(this.ids,this.projectId,this.rightData[0].houseId).then(response => {
        this.getList();
      });
    },

    rightList() {
      this.houseIds = []
      this.rightData.forEach(item=>{
        this.houseIds.push(item.houseId)
        this.projectId.push(item.projectId)
      })
      delRepairDoor(this.ids,this.projectId,this.rightData[0].houseId).then(response => {
        this.getList();
      });
    },

    add(){
      this.houseIds = []
      this.projectId=this.queryParams.projectId
      this.leftData.forEach(item=>{
        this.houseIds.push(item.houseId)
      })
      insertRepairDoor(this.ids,this.projectId,this.leftData[0].houseId).then(response => {
        this.getList();
      });
    },

    handleCalculate() {
      this.reset();
      getCalculate(this.queryParams).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "试算";
      });
    },
    /** 修改按钮操作 */
    handleUpdate() {
      debugger
      this.resetDetail();
      const houseId = this.ids
      const projectId = this.projectId
      getRepairDoor(houseId,projectId).then(response => {
        this.formDetail = response.data;
        this.openDetail = true;
        this.titleDetail = "修改";
      });
    },
    /** 试算 */
    trialCalculateForm() {
      getCalculate(this.form).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "试算";
      });
    },

    /** 核算 */
    checkCalculateForm() {
      debugger
      updateCalculate(this.form).then(response => {
        this.$modal.msgSuccess("核算成功");
        this.open = false;
        this.getList();
      });
    },
    /** 修改 */
    submitForm() {
      updateRepairDoor(this.formDetail).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.openDetail = false;
        this.getList();
      });
    },

  }
};
</script>

<style lang='postcss'>
.left-bar {

  padding: 8px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  height: 100%;


  float: left;
  left: 0%;
  width: 60%;
  height: 100%;
  position: absolute;
  overflow: auto;
  box-shadow: 1px 1px #888888;

}
.right-bar {

  padding: 8px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  height: 100%;

  float: right;
  left: 60%;
  width: 40%;
  height: 100%;
  position: absolute;
  overflow: auto;
}

.shan .mb8{
  margin-top:58px;
}
</style>
